<?php

namespace app\admin\validate;

use app\common\business\Category;
use app\common\lib\Captcha;
use think\Validate;

class Links extends Validate
{
    protected $rule = [
        'id' => 'require',
        'name' => 'require',
        'url' => 'require',
        'category_id' => 'require|checkCategory',
    ];

    protected $scene=[
        'create'=>['name','url','category_id'],
        'update'=>['id','name','url','category_id'],
    ];

    protected $message = [
        'id' => 'ID不能为空',
        'name' => '名称不能为空',
        'url' => '链接不能为空',
        'category_id' => '分类不能为空',
    ];

    protected function checkCategory($value, $rule, $data = [])
    {
        return $this->checkCommonCategory('友情链接分类',$value);
    }
    protected function checkCommonCategory($name,$value)
    {
        try {
            (new Category())->checkValidValueByName($name,$value);
        } catch (\Exception $e) {
            return $e->getMessage();
        }
        return true;
    }
}
